package utils;

import org.junit.Test;

import java.util.Arrays;
import java.util.Random;

@SuppressWarnings("unchecked")
public class ArrayGeneric<T extends Comparable<T>> implements Cloneable {

    private Object[] array;  //维护Object[]类型数组

    @Override
    public ArrayGeneric clone() throws CloneNotSupportedException {
        ArrayGeneric deepclone = (ArrayGeneric)super.clone();
        Object[] arr = this.array.clone();
        deepclone.array=arr;
        return deepclone;
    }

    public ArrayGeneric(int size) {

        array = new Object[size];
    }

    public void put(int index, T item) {
        array[index] = item;
    }

    public T get(int index) {
        return (T) array[index];
    } //数组对象出口强转

    public Object[] getAsArray() {
        return this.array;
    } //数组对象出口强转

    public int length() {
        return this.array.length;
    }

    // 交换两个下标的元素
    public void swap(int m, int n) {
        T temp = (T) array[m];
        array[m] = array[n];
        array[n] = temp;
    }

    @Override
    public String toString() {
        return "ArrayGeneric{" +
                "array=" + Arrays.toString(array) +
                '}';
    }

}